<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="images/xm.jpeg">
    <title>登录页面</title>
    <style>
        body {
            background: url('images/b.jpg') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-module {
            background: rgba(255, 255, 255, 0.8);
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 400px;
            margin: auto;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .login-module h1 {
            color: #007bff;
            font-size: 45px;
            margin-bottom: 20px;
        }
        .login-module input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            box-sizing: border-box;
        }
        .login-module input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            box-sizing: border-box;
        }
        .login-module button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-bottom: 10px;
        }
        .login-module button:hover {
            background-color: #0056b3;
        }
        .login-module a {
            color: #007bff;
            text-decoration: none;
            transition: color 0.3s;
            margin: 5px 0;
        }
        .login-module a:hover {
            color: #0056b3;
        }
    </style>
</head>
<body>
<div class="login-module">
    <h1>Panda熊猫影城</h1>

    <input type="text" id="username" placeholder="请输入用户名"/>
    <input type="password" id="password" placeholder="请输入密码"/>
    <button type="submit" onclick="login()">登录</button>
    <a id="register" href="/templates/register.html">还没有账号？注册</a>

</div>
<script>
    function login() {
        const username = document.getElementById("username").value;
        const password = document.getElementById("password").value;
        if (!username || !password) {
            alert("请输入用户名和密码");
            return;
        }
        fetch('/user/login', {
            method: "POST",
            headers: {
                "Content-Type": "application/json;charset=UTF-8"
            },
            body: JSON.stringify({ username: username, password: password })
        }).then(res => res.text())
            .then(res => {
            if (res) {
                let json = JSON.parse(res);
                console.log(json);
                console.log(json.roleId);
                switch (json.roleId) {
                    case 1:
                        alert("管理员登录成功");

                        location.href = 'http://localhost:8083/templates/Admin_FilmInfo.html';

                        break;
                    case 2:
                        alert("欢迎"+json.username+"登录");
                        location.href = '/templates/index.html';
                        break;
                    default:
                        alert("账号或密码错误");
                        break;
                }
            }
        });
    }
</script>
</body>
</html>
